public class SingleTon3 {
    public static void main(String[] args) {
        try {
            Car3 car3 = new Car3("吉利");
            System.out.println(car3);
            Car3 car4 = new Car3("长安");
            System.out.println(car4);
            Car3 car5 = new Car3("比亚迪");
            System.out.println(car5);
        }catch (IllegalStateException e){
            System.out.println(e.getMessage());
        }
    }
}


class Car3{
    private String name;
    private static int count;
    public Car3(String name){
        this.name=name;
        count++;
        if(count>2){
            throw new IllegalStateException("汽车数量不能超过2");

        }
    }
    public static int getCount(){
        return count;
    }

    @Override
    public String toString() {
        return "Car3{" +
                "name='" + name + '\'' +
                '}';
    }
}